package com.century.sjt.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import com.century.sjt.R;
import com.century.sjt.activity.login.LoginActivity;

/**
 * Created by pig33 on 15-08-22.
 */
public class TipUtil {


    public static ProgressDialog mProgressDialog;

    /**
     * 显示Toast
     * @param tip
     * @param context
     * @param type  0默认显示短消息，其他数字显示长消息
     */

    //避免重复出现
    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };
    private static int duration=3000;  //toast显示时间

    public static int progressDialogCount=0;                                      //用来控制是否已经有ProgressDialog正在显示

    public static void showToast(String tip, Context context, int type){                  //为0显示短消息
//        旧方法
//        try {
//            if (type == 0)
//                Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
//            else Toast.makeText(context, tip, Toast.LENGTH_LONG).show();                //其他显示长消息
//        }catch (Exception e){
//            Log.e(Constant.TAG, "" + e.getMessage());
//        }

        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(tip);
        else {

            if (type == 0)
                mToast = Toast.makeText(context, tip, Toast.LENGTH_SHORT);
            else mToast = Toast.makeText(context, tip, Toast.LENGTH_LONG);                //其他显示长消息
//            mToast = Toast.makeText(context, tip, Toast.LENGTH_LONG);
        }
        mHandler.postDelayed(r, duration);

        mToast.show();

    }

    /**
     * 显示短的Toast消息
     * @param tip
     * @param context
     */
    public static void showToast(String tip,Context context){                  //为0显示短消息
        try {
            showToast(tip, context, 0);
        }catch (Exception e){
            Log.e(Constant.TAG, "" + e.getMessage());
        }
    }

    /**
     * 显示ProgressDialog，无提示。
     * @param s
     * @param context
     */
    public static void openProgressDialog(String s, Context context){
        try {
            mProgressDialog = new ProgressDialog(context);
            mProgressDialog.show();
        }catch (Exception e){
            Log.e(Constant.TAG, "" + e.getMessage());
        }
    }

    /**
     * 显示ProgressDialog，带消息提示。
     * @param title
     * @param message
     * @param context
     * @param type type为1时不能取消，其他数字时可以取消
     */

    //显示小菊花，type=1时不能取消
    public synchronized static void openProgressDialog(String title, String message, Context context, int type){
        if(progressDialogCount==0) {
            try {
                progressDialogCount++;
                if (type == 1)
//                    mProgressDialog = ProgressDialog.show(context, title, message, false, false);
                    ShowProgressDialog(context,message,false);
                else ShowProgressDialog(context,message,true);
//                    mProgressDialog = ProgressDialog.show(context, title, message, false, true);
            } catch (Exception e) {
                Log.e(Constant.TAG, "" + e.getMessage());
            }
        }else{
            progressDialogCount++;
        }
    }

    /**
     * 显示ProgressDialog，带消息提示，不能取消。
     * @param title
     * @param message
     * @param context
     */
    public synchronized static void openProgressDialog(String title,String message,Context context){
        try{
            openProgressDialog(title,message,context,1);
        }catch (Exception e){
            Log.e(Constant.TAG, "" + e.getMessage());
        }
    }

    /**
     * 关闭ProgressDialog
     */

    public synchronized static void closeProgressDialog(){
        try {
            if(progressDialogCount>0) progressDialogCount--;
            if(progressDialogCount==0) {
                if (mProgressDialog != null)
                    mProgressDialog.dismiss();
            }
        }catch (Exception e){
            Log.e(Constant.TAG, "" + e.getMessage());
        }
    }

    public static void log(String typeText,String respone,int isShow){
        try {
            if (isShow==1)
            {
                Log.e(Constant.TAG, typeText+":"+respone);
            }
        }catch (Exception e){
            Log.e(Constant.TAG, "" + e.getMessage());
        }
    }

    public static Boolean isEmpty(String value){
        try {
            if (value.length()==0||value.equals("")){
                    return true;
            }
        }catch (Exception e){
            Log.e(Constant.TAG, "" + e.getMessage());
        }
        return  false;
    }

    public static void showAppayRes(final Context context) {
        //询问是否取消
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(context.getResources().getString(R.string.text_prompt));
        builder.setMessage(context.getResources().getString(R.string.text_remote_login));
        builder.setPositiveButton(context.getResources().getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                SharedPreferences mainSharedPreferences =context.getSharedPreferences(com.century.sjt.util.Constant.TAG, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = mainSharedPreferences.edit();
                editor.clear();
                editor.putString("isLogin", "0");
                editor.commit();
                Intent intent = new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        builder.setCancelable(false);
        builder.create().show();
    }

//    自定义无标题小菊花
    public synchronized static void ShowProgressDialog(Context context,String message,boolean isCanceled){
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setCanceledOnTouchOutside(isCanceled);
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }
}
